package com.yyplatform.frontend.platform.account.service.impl;

import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import com.yyplatform.frontend.platform.account.entity.UserAuthEntity;
import com.yyplatform.frontend.platform.account.entity.UserEntity;
import com.yyplatform.frontend.platform.account.repository.UserAuthRepository;
import com.yyplatform.frontend.platform.account.repository.UserRepository;
import com.yyplatform.frontend.infra.web.jwt.CurrentUser;
import com.yyplatform.frontend.infra.service.UserInfraService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-10-17
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserServiceImpl implements UserInfraService {

    private UserRepository userRepository;
    private UserAuthRepository userAuthRepository;
    private ModelMapper modelMapper;


    @Override
    public CurrentUser getById(long id) {
        UserEntity userEntity = userRepository.getReferenceById(id);
        return modelMapper.map(userEntity, CurrentUser.class);
    }

    @Override
    public boolean checkAuth(long userId, int loginType, String identifier) {
        UserAuthEntity userAuthEntity = userAuthRepository.findByLoginTypeAndIdentifier(loginType, identifier);
        if (userAuthEntity == null) {
            return false;
        }
        if (userAuthEntity.getUserId() != userId) {
            return false;
        }

        return true;
    }


}
